//
// Created by Yurzi on 2021/4/11.
//

#include "UIEngine.h"

/**
 *  窗口先相关
 */
//设置窗口颜色和背景 输入颜色值 前景 背景
void SetColor(UINT uFore,UINT uBack) {
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle,uFore+uBack*0x10);
}
//改变窗口标题 输入标题字符串
void SetTitle(LPCTSTR lpTitle) {
    SetConsoleTitle(lpTitle);
}
//设置缓冲区大小
void SetBufferSize(int x,int y){
    COORD size={x,y};
    HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleScreenBufferSize(handle,size);
}

/**
 *  光标相关
 */
//获取光标信息
void getCursorInfo(COORD *Pos){
    //获得句柄
    HANDLE out= GetStdHandle(STD_OUTPUT_HANDLE);
    //获取缓冲区信息
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if(GetConsoleScreenBufferInfo(out,&csbi)){
        Pos->X=csbi.dwCursorPosition.X;
        Pos->Y=csbi.dwCursorPosition.Y;
    }
}
//改变光标位置
void SetPosC(COORD a){ // set cursor
    HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(out, a);
}
//改变光标位置 输入指定的坐标
void SetPos(int x, int y) { // set cursor2
    COORD pos= {x, y};
    SetPosC(pos);
}
//将光标移动到行首  n代表向上移动几行
void RewindCursor(int n){
    HANDLE out_handle= GetStdHandle(STD_OUTPUT_HANDLE); //获得句柄
    CONSOLE_SCREEN_BUFFER_INFO csbi;//定义缓冲区结构体
    //将光标移动到前端
    if(GetConsoleScreenBufferInfo(out_handle,&csbi)){
        SetPos(0,csbi.dwCursorPosition.Y-n);
    }
}
//设置光标隐藏
void HideConsoleCursor(void) {
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),
                         &cursor_info);
}

/**
 * 图形绘制相关
 */
//绘制一个点
void drawPoint(int x,int y,TCHAR content,UINT uForce,UINT uBack){
    SetColor(uForce,uBack);
    SetPos(x,y);
    wprintf(L"%c",content);
}
//绘制一条2点间直线
int drawLine(int x_begin,int y_begin,int x_end,int y_end,TCHAR content,UINT uForce,UINT uBack){

    int temp=0;
    //如果垂直与x轴
    if(x_end==x_begin){
        if (y_begin>y_end){
            temp=y_end;
            y_end=y_begin;
            y_begin=temp;
        }
        for (int i = y_begin; i <=y_end; ++i)
        {
            drawPoint(x_begin,i,content,uForce,uBack);
        }
        return ALLISOK;
    }

    //求出直线解析式
    double k=0;
    double b=0;
    k=(double)(y_end-y_begin)/(x_end-x_begin);
    b=(double)y_end-k*x_end;
    //大小判断并交换
    if(x_begin>x_end){
        temp=x_end;
        x_end=x_begin;
        x_begin=temp;
    }
    if (y_begin>y_end){
        temp=y_end;
        y_end=y_begin;
        y_begin=temp;
    }
    //绘制直线
    int y=0;
    if(doublecmp(k,0)==0){
        for(int i=x_begin;i<=x_end;i++){
            drawPoint(i,y_begin,content,uForce,uBack);
        }
    } else {
        for (int i = x_begin; i <= x_end; ++i) {
            y = (int) (k * i + b);
            drawPoint(i, y, content, uForce, uBack);
        }
    }
    return ALLISOK;
}
//绘制一个圆
int drawCircle(int x,int y,int r,int dergee_begin,TCHAR content,UINT uForce,UINT uBack,int ctrl){
    int add=0;
    int jingdu=10;
    //模式控制
    switch (ctrl) {
        case 0:
            add=360*jingdu;    //绘制整圆
            break;
        case 1:
            add=180*jingdu;    //绘制半圆
            break;
        case 2:
            add=90*jingdu;     //绘制四分之一圆
            break;
        case 3:
            add=45*jingdu; //绘制八分之一圆
            break;
        default:
            add=360*jingdu;    //默认整圆
    }

    int rate=180*jingdu;    //转化比例
    int begin=dergee_begin*jingdu;  //循环起点
    int end=begin+add;  //循环结束
    //绘制圆
    for(int i=begin;i<=end;i++) {
        drawPoint((int) (x + (r * 2.06) * cos(3.1415926 * i / rate) + 0.5),
                  (int) (y + r * sin(3.1415926 * i / rate) + 0.5), content, uForce, uBack);
    }
    return ALLISOK;
}
//绘制一个矩形
int drawRecj(int x_begin,int y_begin,int width,int length,TCHAR contentline,TCHAR contentrow,TCHAR contentangle,UINT uForce,UINT uBack){
    //计算出四点坐标
    int x_right=0,x_bottom=0,x_end=0;
    int y_right=0,y_bottom=0,y_end=0;
    x_right=x_begin+width;y_right=y_begin;
    x_bottom=x_begin;y_bottom=y_begin+length;
    x_end=x_begin+width;y_end=y_begin+length;

    //绘制4角
    drawPoint(x_begin,y_begin,contentangle,uForce,uBack);
    drawPoint(x_right,y_right,contentangle,uForce,uBack);
    drawPoint(x_bottom,y_bottom,contentangle,uForce,uBack);
    drawPoint(x_end,y_end,contentangle,uForce,uBack);
    //绘制 4边
    drawLine(x_begin+1,y_begin,x_right-1,y_right,contentline,uForce,uBack); //绘制横线1
    drawLine(x_bottom+1,y_bottom,x_end-1,y_end,contentline,uForce,uBack);   //绘制横线2
    drawLine(x_begin,y_begin+1,x_bottom,y_bottom-1,contentrow,uForce,uBack);    //绘制竖线1
    drawLine(x_right,y_right+1,x_end,y_end-1,contentrow,uForce,uBack);  //绘制竖线2
    return ALLISOK;
}

//画一个表格
void drawExcel(int* ax, int* ay, int nx, int ny, UINT uForce, UINT uBack) {
    for (int x = 0; x < nx; x++) {
        drawLine(ax[x], ay[0], ax[x], ay[ny - 1], L'|', uForce, uBack);
    }
    for (int y = 0; y < ny; y++) {
        drawLine(ax[0], ay[y], ax[nx - 1], ay[y], L'-', uForce, uBack);
    }
    for (int x = 0; x < nx; x++) {
        for (int y = 0; y < ny; y++) {
            drawPoint(ax[x], ay[y], L'+', uForce, uBack);
        }
    }
}

//将指定矩形区域清空
void drawWhite(int x1,int y1,int x2,int y2) {
    SetColor(15, 15);
    for (int i = y1; i <=y2;i++) {
        drawLine(x1,i,x2,i, L' ', 15, 15);
    }
    SetColor(1, 15);
}

//在指定矩形中输出中文长字符串（标点加空格配一对）
void printfInRecj(int x1, int y1, int x2, int y2, const char* s) {
    const char* p = s;
    while (*p != 0) {
        SetPos(x1, y1++);
        for (int i = 0; i <= x2 - x1;) {
            if (*p == 0)break;
            printf("%-2.2s", p);
            p++;
            if (*p != 0)p++;
            i += 2;
        }
    }
}

//在指定位置输出字符串
void printText(int x,int y,const TCHAR *str,UINT uForce,UINT uBack){
    SetPos(x,y);
    SetColor(uForce,uBack);
    printf("%s",str);
}

//换行输出字符串并拓宽表格//xw右边界坐标,x,y表示光标位置//返回最后文字的y坐标
int PrtTagName(Car* hc, int x0, int y0, int xw) {
    int i = 0;
    int namel = 0;
    int x = x0, y = y0;
    while(i<10) {
        if (hc->condiment[i].tagId > 0) {
            SetPos(x, y);

            namel = strlen(hc->condiment[i].tagName) + 1;
            if (namel >= xw - x0){
                i++;
                continue;
            }
            x += namel;
            if (x >= xw) {
                x = x0;
                y++;
                continue;
            }

            printf("%s ", hc->condiment[i].tagName);
        }
        i++;
    }
    return y;
}
/**
 *  用户输入区相关
 */
//清除一行输入
void ClearOneLine(){
    RewindCursor(0);
    puts("                                                                                                                                ");
    RewindCursor(1);
}
//为循环的输出清除用户输入
void ForCyclePrint(){
    /*为可能多次显示的语句做准备 注意：该语句因末尾有回车
    例程：
    printf("2333\n");
    getchar();
    ForCyclePrint();
    printf("2333\n");
    getchar();
    getchar();
    */
    RewindCursor(1);
    ClearOneLine(); //清除用户输入
    RewindCursor(1);
    ClearOneLine(); //前一次是输出
}
//清空输入缓冲区
void FlushInputBuffer(){
    while (getchar()!='\n');
}